<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <link href="main.css" rel="stylesheet" type="text/css"/>
    <script src='socket.io.js'></script>
    <script src='jquery.js'></script>
    <script src='notify.js'></script>
</head>
<body>

<button class="send">发送</button>

<div class="notification sticky hide">
    <p id="content"></p>
    <a class="close" href="javascript:"> <img src="/icon-close.png"/></a>
</div>

<center id="online_box"></center>

<script>
    $(document).ready(function () {

//        var uid = 7;
//        // 连接服务端
//        var socket = io('https://socket.uxuj.cn:4331');
//
//        // 连接后登录
//        socket.on('connect', function () {
//            socket.emit('login', uid);
//        });
//        // 后端推送来消息时
//        socket.on('new_msg', function (msg) {
//            $('#content').html('收到消息：' + msg);
//            $('.notification.sticky').notify();
//        });
//        // 后端推送来在线数据时
//        socket.on('update_online_count', function (online_stat) {
//            $('#online_box').html(online_stat);
//        });

        var uid = "crm_18911621396_app";
        var to = "crm_18911621396_web";

        // 证书是会检查域名的，请使用域名连接
        ws = new WebSocket("wss://socket.uxuj.cn:4333/wss");

        //登录
        ws.onopen = function () {
            ws.send('{"type":"login","uid":"' + uid + '"}');
        };

        //监听
        ws.onmessage = function (e) {
            var json_data = JSON.parse(e.data);
            if (json_data.type != 'ping') {
                if (json_data.content && json_data.content.type == 'login' && json_data.content.code == 0) {
                    console.log('已在其他设备登录');
                    window.clearInterval(ping);
                    ws.close();
                } else {
                    console.log("收到服务端的消息：", json_data);
                }
            }
        };

        //心跳
        var ping = setInterval(function () {
            ws.send('{"type":"ping"}');
        }, 5000);

        //发送
        $('.send').click(function () {
            ws.send('{"type":"publish","to":"' + to + '","content":"test"}');
        })

    });
</script>
</body>
</html>
